package com.sxm.yiwei.tools;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.AppManager;
import com.sxm.yiwei.activitis.BookAddPriceActivity;
import com.sxm.yiwei.activitis.CryActivity;
import com.sxm.yiwei.activitis.MineAccount;
import com.sxm.yiwei.activitis.MineMember;
import com.sxm.yiwei.activitis.MsgMsgActivity;
import com.sxm.yiwei.activitis.OrderPickupActivity;
import com.sxm.yiwei.activitis.VendorNoServiceActivity;
import com.sxm.yiwei.activitis.YiViListActivity;
import com.sxm.yiwei.beans.pinche.Order;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.pinche.CarVerifyActivity;
import com.sxm.yiwei.pinche.PincheOrderDetailActivity;
import com.sxm.yiwei.views.CenterDialog;

import java.util.List;

/**
 * Created by KuKa on 2017/8/1.
 * 弹窗管理类
 *
 */

public class DialogMain {

    /**
     * 抢单
     * @param context 2003
     * @param data
     * @param dis
     */
    public static void dialogToast(final Context context, Bundle data, String dis){
        AlertDialog.Builder builder = null;
        final String type1;
        String message = data.getString("message");
        String extras = data.getString("extras");
        JSONObject obj = JSON.parseObject(extras);
        String total_amount = obj.getString("total_amount");
        String longitude = obj.getString("longitude");
        String latitude = obj.getString("latitude");
        String type = obj.getString("type");
        String address = obj.getString("address");
        final String orderId = obj.getString("orderId");

        if (builder!=null){

        }
        builder =  new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View dia = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.book_dialog, null);
        builder.setView(dia);
        TextView title = (TextView) dia.findViewById(R.id.book_dialog_title);
        TextView dialogDispatch = (TextView) dia.findViewById(R.id.book_dialog_dispatch);
        TextView dialogPrice = (TextView) dia.findViewById(R.id.book_dialog_price);
        TextView dialogAddress = (TextView) dia.findViewById(R.id.book_dialog_address);
        Button dialogCancel = (Button) dia.findViewById(R.id.book_dialog_cancel);
        Button dialogSure = (Button) dia.findViewById(R.id.book_dialog_sure);
        dialogDispatch.setText(dis);
        title.setText(message);
        dialogPrice.setText(total_amount);
        dialogAddress.setText(address);
        final AlertDialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialogSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,CryActivity.class);
                intent.putExtra("id",orderId);
                context.startActivity(intent);
                dialog.dismiss();

            }
        });
        dialog.show();

    }

    /**
     * 账户信息
     * @param context
     * @param data Jpush 信息
     */
    public static void amountDialog(final Context context, Bundle data){
        String message1 = data.getString("message");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.amount_dialog, null);
        builder.setView(view);
        Button cancel = (Button) view.findViewById(R.id.amount_cancel);
        Button sure = (Button) view.findViewById(R.id.amount_sure);
        TextView message = (TextView) view.findViewById(R.id.amount_message);
        TextView title = (TextView) view.findViewById(R.id.amount_title);
        title.setText("提示");
        message.setText(message1);
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context,MineAccount.class));
            }
        });
        dialog.show();

    }


    /**
     * 超时未抢单
     * @param context
     * @param data 2002
     */

    public static void outTimeDialog(final Context context, Bundle data){
        String message1 = data.getString("message");
        String extras = data.getString("extras");
        JSONObject obj1 = JSON.parseObject(extras);
        final String orderId = obj1.getString("orderId");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.outtime_dialog, null);
        builder.setView(view);
        final Button cancel = (Button) view.findViewById(R.id.outtime_cancel);
        Button sure = (Button) view.findViewById(R.id.outtime_sure);
        TextView title = (TextView) view.findViewById(R.id.outtime_title);
        TextView message = (TextView) view.findViewById(R.id.outtime_message);
        title.setText("超时未抢单");
        message.setText(message1);
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context,BookAddPriceActivity.class);
                intent.putExtra("id",orderId);
                intent.putExtra("from","11");
                context.startActivity(intent);
                dialog.dismiss();

            }
        });
        dialog.show();
    }

    /**
     * 订单加价 2004
     * @param context
     */
    public static void bookAddPrice(final Context context,Bundle data){
        String message1 = data.getString("message");
        String extras = data.getString("extras");
        JSONObject object = JSON.parseObject(extras);
        String type = object.getString("type");
        String orderId = object.getString("orderId");
        String serviceName = object.getString("serviceName");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.bookaddprice_dialog, null);
        builder.setView(view);
        final Button cancel = (Button) view.findViewById(R.id.addprice_cancel);
        Button sure = (Button) view.findViewById(R.id.addprice_sure);
        TextView message = (TextView) view.findViewById(R.id.addprice_message);
        TextView title = (TextView) view.findViewById(R.id.addprice_title);
        title.setText(message1);
        message.setText(serviceName);
        sure.setText("查看");
        builder.setView(view);
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                context.startActivity(new Intent(context,MsgMsgActivity.class));
                dialog.dismiss();

            }
        });
        dialog.show();
    }
    /**
     * 抢单提醒
     *
     */
    public static void cryBookMessage(final Context context,Bundle data){

        String message1 = data.getString("message");
        String extras = data.getString("extras");
        JSONObject object = JSON.parseObject(extras);
        final String type = object.getString("type");
        final String orderId = object.getString("orderId");
        final String distance = object.getString("distance");
        final String nickname = object.getString("nickname");
        final String gradeCode = object.getString("gradeCode");
        final String serviceName = object.getString("serviceName");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.amount_dialog, null);
        builder.setView(view);
        Button cancel = (Button) view.findViewById(R.id.amount_cancel);
        Button sure = (Button) view.findViewById(R.id.amount_sure);
        final TextView message = (TextView) view.findViewById(R.id.amount_message);
        TextView title = (TextView) view.findViewById(R.id.amount_title);
        title.setText(message1);
        if (type.equals("2008")){
            message.setText(serviceName+" 被人购买 快去处理吧");
        }else if (type.equals("2005")){
            if (distance==null){
                message.setText("你已被选定 快去取货吧");
            }else {
                message.setText(nickname+" 抢了您的订单");
            }

        }
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("2008")){
                    Intent intent = new Intent(context,VendorNoServiceActivity.class);
                    context.startActivity(intent);
                }else if (type.equals("2005")){
                    if (gradeCode==null){
                        Intent i = new Intent(context,OrderPickupActivity.class);
                        i.putExtra("id",orderId);
                        context.startActivity(i);
                    }else {
                        Intent in = new Intent(context,YiViListActivity.class);
                        in.putExtra("id",orderId);
                        in.putExtra("distance",distance);
                        in.putExtra("nickname",nickname);
                        in.putExtra("gradeCode",gradeCode);
                        context.startActivity(in);
                    }

                }

                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 收入
     * @param context
     * @param data
     *
     */
    public static void moneyDialog(final Context context,Bundle data){
        String extras = data.getString("extras");
        JSONObject object = JSON.parseObject(extras);
        String incomeAmount = object.getString("incomeAmount");
        String message1 = data.getString("message");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.amount_dialog, null);
        builder.setView(view);
        Button cancel = (Button) view.findViewById(R.id.amount_cancel);
        Button sure = (Button) view.findViewById(R.id.amount_sure);
        TextView message = (TextView) view.findViewById(R.id.amount_message);
        TextView title = (TextView) view.findViewById(R.id.amount_title);
        title.setText(message1);
         message.setText("你有一笔资金进账 "+incomeAmount+" 元");
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                context.startActivity(new Intent(context,MineAccount.class));
            }
        });
        dialog.show();

    }

    /**
     *销售等级提升
     */

    public static final void SellDialog(final Context context,Bundle data){
        String message1 = data.getString("message");
        String extras = data.getString("extras");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }
       builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.amount_dialog, null);
        builder.setView(view);
        Button cancel = (Button) view.findViewById(R.id.amount_cancel);
        Button sure = (Button) view.findViewById(R.id.amount_sure);
        TextView message = (TextView) view.findViewById(R.id.amount_message);
        TextView title = (TextView) view.findViewById(R.id.amount_title);
        title.setText(message1);
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    //拼车
    public static final void outMoneyDia(final Context context,Bundle data){
        String message1 = data.getString("message");
        String extras = data.getString("extras");
        JSONObject object = JSON.parseObject(extras);
        String message = object.getString("message");
        AlertDialog.Builder builder = null;
        if (builder!=null){

        }
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.amount_dialog, null);
        builder.setView(view);
        Button cancel = (Button) view.findViewById(R.id.amount_cancel);
        Button sure = (Button) view.findViewById(R.id.amount_sure);
        TextView messaget = (TextView) view.findViewById(R.id.amount_message);
        TextView title = (TextView) view.findViewById(R.id.amount_title);
        title.setText("提示");
        messaget.setText(message);
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }


    //拼车弹框
    /*
    * 被退票推送dialog
    * */
    public static final void onRefundedDialog(final Context context, Bundle data) {
        AlertDialog.Builder builder = null;
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.dialog_layout, null);
        builder.setView(view);
        TextView cancel = (TextView) view.findViewById(R.id.dialog_cancel);
        TextView sure = (TextView) view.findViewById(R.id.dialog_sure);
        TextView message = (TextView) view.findViewById(R.id.dialog_text);
        sure.setText("知道了");
        cancel.setText("查看订单");
        message.setText("非常抱歉，车主已将订单退票\n[杭州]-[上海]\n订单号：00000000");
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AppManager.getAppManager().currentActivity(), PincheOrderDetailActivity.class);
                intent.putExtra("order_type",0);
                intent.putExtra("go_type",0);
                intent.putExtra("position",0);
                intent.putExtra("order",new Order());
                AppManager.getAppManager().currentActivity().startActivity(intent);
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    /*
    * 被抢单推送dialog
    * */
    public static final void onTookDialog(final Context context, Bundle data) {
        AlertDialog.Builder builder = null;
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.dialog_layout, null);
        builder.setView(view);
        TextView cancel = (TextView) view.findViewById(R.id.dialog_cancel);
        TextView sure = (TextView) view.findViewById(R.id.dialog_sure);
        TextView message = (TextView) view.findViewById(R.id.dialog_text);
        sure.setText("知道了");
        cancel.setText("查看订单");
        message.setText("非常抱歉，车主已将订单退票\n[杭州]-[上海]\n出发时间：2017-12-12 12:00\n订单号：00000000");
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AppManager.getAppManager().currentActivity(), PincheOrderDetailActivity.class);
                intent.putExtra("order_type",0);
                intent.putExtra("go_type",0);
                intent.putExtra("position",0);
                intent.putExtra("order",new Order());
                AppManager.getAppManager().currentActivity().startActivity(intent);
                dialog.dismiss();
            }
        });
        dialog.show();
    }


    /*
    * 超时或订单被抢走时的弹框提示
    * */
    public static void tipDialog(String text){
        AlertDialog.Builder builder = null;
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        builder.setCancelable(true);
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.tip_dialog, null);
        builder.setView(view);
        TextView know = (TextView) view.findViewById(R.id.know);
        TextView message = (TextView) view.findViewById(R.id.dialog_text);
        know.setText("知道了");
        message.setText(text);
        final AlertDialog dialog = builder.create();
        know.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
    /*
    * 车辆认证
    * */
    public static final void carVerifyDialog(String text, String negative, String positive, final int failure_code) {
        AlertDialog.Builder builder = null;
        builder = new AlertDialog.Builder(AppManager.getAppManager().currentActivity());
        View view = LayoutInflater.from(AppManager.getAppManager().currentActivity()).inflate(R.layout.dialog_layout, null);
        builder.setView(view);
        TextView cancel = (TextView) view.findViewById(R.id.dialog_cancel);
        TextView sure = (TextView) view.findViewById(R.id.dialog_sure);
        TextView message = (TextView) view.findViewById(R.id.dialog_text);
        sure.setText(positive);
        cancel.setText(negative);
        message.setText(text);
        final AlertDialog dialog = builder.create();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (failure_code){
                    case Constant.NAME_VERIFYING:
                        break;
                    case Constant.NAME_NOT_VERIFY:
                        AppManager.getAppManager().currentActivity().startActivity(new Intent(AppManager.getAppManager().currentActivity(), MineMember.class));
                        break;
                    case Constant.NAME_VERIFY_FALSE:
                        AppManager.getAppManager().currentActivity().startActivity(new Intent(AppManager.getAppManager().currentActivity(), MineMember.class));
                        break;
                    case Constant.CAR_VERIFYING:
                        break;
                    case Constant.CAR_VERIFY_FALSE:
                        AppManager.getAppManager().currentActivity().startActivity(new Intent(AppManager.getAppManager().currentActivity(),CarVerifyActivity.class));
                        break;
                    case Constant.CAR_NOT_VERIFY:
                        AppManager.getAppManager().currentActivity().startActivity(new Intent(AppManager.getAppManager().currentActivity(),CarVerifyActivity.class));

                        break;
                    default:
                        break;
                }
                dialog.dismiss();
            }
        });
        dialog.show();
    }

}
